home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DDJMAG / DDJ9207.ZIP / AVKCAPT.ZIP / LOG.C < prev    next >
C/C++ Source or Header  |  1992-03-11  |  7KB  |  306 lines

  1. //-------------------------------------------------------------------------
  2. //            ActionMedia II Programmer's Toolkit
  3. //            
  4. //            Windows Sample Code Shared Functions
  5. //
  6. // Module Name:    log.c
  7. //
  8. // Description:    This module contains functions for creating a log file
  9. //                and writing log messages to it.
  10. //
  11. //    Copyright Intel Corp. 1991, 1992
  12. //    All Rights Reserved.
  13. //
  14. //-------------------------------------------------------------------------
  15. //
  16. // Exported functions from this module:
  17. //
  18. //        LogOpen
  19. //        LogWrite
  20. //        LogMsg
  21. //        LogClose
  22. //        LogOn
  23. //        LogOff
  24. //        LogStat
  25. //
  26. //-------------------------------------------------------------------------
  27. // NOTES on Log
  28. //    
  29. //    These functions are used in most of the sample applications to 
  30. //    manage a log file for monitoring an application's performance and
  31. //    for debugging.
  32. //
  33. //-------------------------------------------------------------------------
  34. #include <windows.h>
  35. #include <stdio.h>
  36. #include <string.h>
  37. #include <stdarg.h>
  38. #include <io.h>
  39. #include <fcntl.h>
  40. #include <errno.h>
  41. #define    LOG_NOEXTERNS
  42. #include "log.h"
  43.  
  44. static int        hFile = -1;
  45. static int        fLogFlag = FALSE;
  46. static char        Buf[256];
  47.  
  48. //-------------------------------------------------------------------------
  49. //FUNCTION:
  50. //
  51. //    BOOL    LogOpen(pFileSpec)
  52. //
  53. //PARMS IN:
  54. //    
  55. //    char *pFileSpec        pointer to string with file name to use in
  56. //                            creating the log file. 
  57. //    
  58. //DESCRIPTION:
  59. //    
  60. //    This function creates a log file with the passed in name for use by 
  61. //    the logging subsystem. If a file already exists with the name, it
  62. //    will be truncated. 
  63. //
  64. //    There may only be one log file open at a time.
  65. //    
  66. //RETURN:
  67. //    
  68. //    TRUE on success.
  69. //    FALSE on failure (no error message is displayed to the user).
  70. //    
  71. //-------------------------------------------------------------------------
  72.  
  73. BOOL 
  74. LogOpen(char *pFileSpec)
  75. {
  76.     OFSTRUCT    Of;
  77.  
  78.     //    If the log file is not opened, open it now.
  79.  
  80.     if (hFile == -1)
  81.     {
  82.         if ((hFile = OpenFile((LPSTR)pFileSpec, (LPOFSTRUCT)&Of, 
  83.           OF_CREATE | OF_WRITE | OF_SHARE_EXCLUSIVE)) == -1)
  84.             return FALSE;
  85.     }
  86.     fLogFlag = TRUE;
  87.     return TRUE;
  88. }
  89.  
  90. //-------------------------------------------------------------------------
  91. //FUNCTION:
  92. //
  93. //    BOOL    LogWrite(char *pFormat, ...)
  94. //
  95. //PARMS IN:
  96. //    
  97. //    char *pFormat        Format string similar to that used by printf().
  98. //    
  99. //DESCRIPTION:
  100. //    
  101. //    This function writes out a message to the log file.  It works like
  102. //    printf() taking a format string and a variable number of values
  103. //    to be formatted. It does NOT append a newline. This is left to 
  104. //    the application. The manifest constant CR, which is #defined in  
  105. //    log.h, can be concatenated as follows:
  106. //
  107. //        LogWrite("Error code = %d"CR, ErrCode);
  108. //
  109. //    Note that ANSI string concatenation is used to add CR to the 
  110. //    format string rather than using a separate format argument.
  111. //    
  112. //RETURN:
  113. //    
  114. //    TRUE on success.
  115. //    FALSE on failure (no error message is displayed to the user).
  116. //    
  117. //-------------------------------------------------------------------------
  118.  
  119. BOOL 
  120. LogWrite(char *pFormat, ...)
  121. {
  122.     va_list    pArgs;
  123.     int        hDup;
  124.  
  125.     //    Logging off, just leave without an error.
  126.  
  127.     if (!fLogFlag)
  128.         return TRUE;
  129.  
  130.     //    Logging on, but log file not opened. This is an error.
  131.  
  132.     if (hFile == -1)
  133.         return FALSE;
  134.  
  135.     //    Set up to process the variable number of arguments.
  136.  
  137.     va_start(pArgs, pFormat);
  138.  
  139.     //    Format the arguments and string.
  140.  
  141.     vsprintf(Buf, pFormat, pArgs);
  142.     
  143.     //    Write out the formatted string.
  144.  
  145.     _lwrite(hFile, (LPSTR)Buf, strlen(Buf));
  146.  
  147.     //    Get a duplicate handle to the log file and close it.  This is
  148.     //    a way to force a flush of the internal file buffer to the disk
  149.     //    so a subsequent crash will not lose the message.
  150.  
  151.     if ((hDup = dup(hFile)) != -1)
  152.         _lclose(hDup);
  153.  
  154.     //    Clean up the variable argument processing.
  155.  
  156.     va_end(pArgs);
  157.  
  158.     return TRUE;
  159. }
  160.  
  161. //-------------------------------------------------------------------------
  162. //FUNCTION:
  163. //
  164. //    BOOL    LogMsg(pMsg)
  165. //
  166. //PARMS IN:
  167. //    
  168. //    char *pMsg            pointer to the message string to be written 
  169. //                            to the log file.
  170. //    
  171. //DESCRIPTION:
  172. //    
  173. //    This function writes out a message to the log file. It is a simple
  174. //    function that can be used when the application doesn't need to 
  175. //    include data from variables.  This function DOES append a newline
  176. //    to the caller's string.
  177. //
  178. //RETURN:
  179. //    
  180. //    TRUE on success.
  181. //    FALSE on failure (no error message is displayed to the user).
  182. //    
  183. //-------------------------------------------------------------------------
  184.  
  185. BOOL 
  186. LogMsg(char *pMsg)
  187. {
  188.     int        hDup;
  189.  
  190.     //    Logging off, just leave without an error.
  191.     
  192.     if (!fLogFlag)
  193.         return TRUE;
  194.  
  195.     //    Logging on, but log file not opened. This is an error.
  196.  
  197.     if (hFile == -1)
  198.         return FALSE;
  199.  
  200.     //    Concatenate the newline.
  201.  
  202.     sprintf(Buf, "%s"CR, pMsg);
  203.  
  204.     //    Write it to the log file.
  205.  
  206.     _lwrite(hFile, (LPSTR)Buf, strlen(Buf));
  207.  
  208.     //    Flush the file as we did in LogWrite().
  209.  
  210.     if ((hDup = dup(hFile)) != -1)
  211.         close(hDup);
  212.  
  213.     return TRUE;
  214. }
  215.  
  216. //-------------------------------------------------------------------------
  217. //FUNCTION:
  218. //
  219. //    BOOL LogClose(VOID)
  220. //
  221. //DESCRIPTION:
  222. //    
  223. //    Closes the log file.
  224. //    
  225. //RETURN:
  226. //    
  227. //    TRUE on success.
  228. //    FALSE on failure (no error message is displayed to the user).
  229. //    
  230. //-------------------------------------------------------------------------
  231.  
  232. BOOL 
  233. LogClose()
  234. {
  235.     if (hFile != -1)
  236.     {
  237.         if (_lclose(hFile))
  238.             return FALSE;
  239.         hFile = -1;
  240.     }
  241.  
  242.     fLogFlag = FALSE;
  243.     return TRUE;
  244. }
  245.  
  246. //-------------------------------------------------------------------------
  247. //FUNCTION:
  248. //
  249. //    VOID LogOn(VOID)
  250. //
  251. //DESCRIPTION:
  252. //    
  253. //    Turns logging on. Can be used in conjunction with LogOff() to turn
  254. //    loggin on and off within an application.
  255. //    
  256. //-------------------------------------------------------------------------
  257.  
  258. VOID 
  259. LogOn()
  260. {
  261.     if (hFile != -1)
  262.         fLogFlag = TRUE;
  263. }
  264.  
  265. //-------------------------------------------------------------------------
  266. //FUNCTION:
  267. //
  268. //    VOID LogOff(VOID)
  269. //
  270. //DESCRIPTION:
  271. //    
  272. //    Turns logging off.
  273. //    
  274. //-------------------------------------------------------------------------
  275.  
  276. VOID 
  277. LogOff()
  278. {
  279.     if (hFile != -1)
  280.         fLogFlag = FALSE;
  281. }
  282.  
  283. //-------------------------------------------------------------------------
  284. //FUNCTION:
  285. //
  286. //    BOOL    LogStat(VOID)
  287. //
  288. //DESCRIPTION:
  289. //    
  290. //    Returns the status of logging (on or off).
  291. //    
  292. //RETURN:
  293. //    
  294. //    TRUE if logging is on.
  295. //    FALSE if logging is off.
  296. //    
  297. //-------------------------------------------------------------------------
  298.  
  299. BOOL 
  300. LogStat()
  301. {
  302.     return fLogFlag;
  303. }
  304.  
  305.  
  306.